<?php

/**
 * createad.php
 * @package adServer
 * @filesource
 */

/*	Close the backdoor	*/
$curPage = strtoupper($_SERVER['PHP_SELF']);
if (isset($curPage) && strpos($curPage, 'INDEX.PHP') === false)
    header('Location:./index.php');


require_once (CLASS_PATH . 'template.class.php');

/* Load the side menu */
$sideMenu = file_get_contents(TEMPLATE_PATH . 'sideMenu.tpl');

/*	load the page header	*/
$header = new template(TEMPLATE_PATH . 'header.tpl');
$header->replace('&&SIDEMENU&&', $sideMenu);
$header->replace('&&STYLE&&', $session->loggedIn ? "style='display:block;'" : "");
$header->replace('&&USER&&', $session->username);

/*	Load the page content	*/
$pageContent = new template(TEMPLATE_PATH . $nextAction . '.tpl');

/*	Load the options fropm Db */

$adTypes = $session->dbHandle->get_results(SQL_GET_TYPES);
$adLanguages = $session->dbHandle->get_results(SQL_GET_LANGUAGES);
$adCampaigns = $session->dbHandle->get_results(SQL_GET_CAMPAIGNS);

/* Create select options */
$adTypesHtml = '';
$selected = '';
foreach ($adTypes as $result) {
    if (isset($_GET['adType']) && $_GET['adType'] == $result['typeId'])
        $selected = 'selected="selected"';
    else
        $selected = '';
    $adTypesHtml .= "<option $selected value ='" . $result['typeId'] . "'>" . $result['typeName'] .
        '</option>';
}

/* Create select options */
$adLanguagesHtml = '';

foreach ($adLanguages as $result) {
    if (isset($_GET['adLanguage']) && $_GET['adLanguage'] == $result['code'])
        $selected = 'selected="selected"';
    elseif(!isset($_GET['adLanguage'])&& $result['code']=='--') $selected = 'selected="selected"';
    else
        $selected = '';
    $adLanguagesHtml .= "<option $selected value ='" . $result['code'] . "'>" .
        $result['language'] . '</option>';
}

/* Create select options */
$adStartOffsetHtml = '';
for ($a = 0; $a <= 100; $a = $a + 10) {
    if (isset($_GET['adStartOffset']) && (int)$_GET['adStartOffset'] == $a)
        $selected = 'selected="selected"';
    else
        $selected = '';
    $adStartOffsetHtml .= "<option $selected value ='" . $a . "'>" . $a .
        ' %</option>';
}

/* Create select options */
$adCampaignsHtml = '';
foreach ($adCampaigns as $result) {
    if (isset($_GET['adCampaign']) && $_GET['adCampaign'] == $result['campaign'])
        $selected = 'selected="selected"';
    else
        $selected = '';
    $adCampaignsHtml .= "<option $selected value ='" . $result['campaign'] .
        "'>" . $result['campaign'] . '</option>';
}


/* Populate the template */
$pageContent->replace('&&ERRMSG&&', urldecode($_GET['msg']));
$pageContent->replace('&&ADTYPE&&', $adTypesHtml);
$pageContent->replace('&&ADLANGUAGES&&', $adLanguagesHtml);
$pageContent->replace('&&ADSTART&&', $adStartOffsetHtml);
$pageContent->replace('&&ADCAMPAIGN&&', $adCampaignsHtml);
$pageContent->replace('&&ADTYPEVAL&&', $_GET['adType']);
$pageContent->replace('&&ADDURATIONVAL&&', $_GET['adDuration']);
$pageContent->replace('&&ADHTMLVAL&&', $_GET['adHtml']);

if (isset($_GET['adSuccess']))
    $success = 'AdUnit Created Successfully';
else
    $success = '';
$pageContent->replace('&&SUCCESSMSG&&', $success);

/*	Echo the page	*/
echo $header->templateHtml;
echo $pageContent->templateHtml;

?>